package com.qingxin.wx.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.Manufacturer;
import com.qingxin.park.entity.ParkBlock;
import com.qingxin.park.entity.ParkBlockPlantView;
import com.qingxin.park.entity.ProRecord;
import com.qingxin.park.entity.ProRecordView;
import com.qingxin.park.entity.ProductionLink;
import com.qingxin.park.entity.ProductionyzLink;
import com.qingxin.park.service.CompanyService;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.park.service.ManufacturerService;
import com.qingxin.park.service.ParkBlockPlantService;
import com.qingxin.park.service.ParkBlockService;
import com.qingxin.park.service.ProRecordService;
import com.qingxin.park.service.ProductionLinkService;
import com.qingxin.park.service.ProductionyzLinkService;
import com.qingxin.park.vo.TraceCompany;
import com.qingxin.park.vo.TracePark;
import com.qingxin.repertory.entity.Repertory;
import com.qingxin.repertory.service.RepertoryService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.entity.SysUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.SysGroupService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.DateUtil;
import com.qingxin.sys.utils.JsonUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.sys.utils.UploadUtil;
import com.qingxin.ui.bean.TreeNode;
import com.qingxin.web.service.TraceQueryService;


/**
 * app接口
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/api/app")
public class AppAction extends BaseAction {
	
	@Autowired
	private CompanyService companyService;
	@Autowired
	private FarmParkService farmParkService;
	@Autowired
	private ParkBlockService parkBlockService;
	@Autowired
	private ManufacturerService manufacturerService;
	
	@Autowired
	private TraceQueryService traceQueryService;

	@Autowired
	private SysGroupService sysGroupService;
	
	@Autowired
	private ParkBlockPlantService parkBlockPlantService;
	@Autowired
	private ProRecordService proRecordService;
	
	@Autowired
	private RepertoryService repertoryService;
	
	@Autowired
	private ProductionLinkService productionLinkService;
	@Autowired
	private ProductionyzLinkService productionyzLinkService;

	/**
	 * 登录
	 * @param loginName
	 * @param loginPwd
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/login.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object login(String loginName, String loginPwd, HttpServletRequest request){
		Map<String, Object> rsmap = new HashMap<>();
		
		if(StringUtils.isEmpty(loginName) || StringUtils.isEmpty(loginPwd)){
			rsmap.put("code", 1);
			rsmap.put("message", "登录失败！");
			return rsmap;
		}
		SysUser sysUser = sysUserService.getByProperty("login_name", loginName);
		if(sysUser == null){
			rsmap.put("code", 1);
			rsmap.put("message", "登录失败！");
			return rsmap;
		}
		
		loginPwd = StringUtils.md5(loginPwd+sysUser.getSalt());
		if(!sysUser.getPassword().equalsIgnoreCase(loginPwd)){
			rsmap.put("code", 1);
			rsmap.put("message", "登录失败！");
			return rsmap;
		}
		
		rsmap.put("code", 200);
		rsmap.put("message", "登录成功！");
		rsmap.put("ssid", sysUser.getUserid());//session id（用户ID）
		
		Map<String, Object> data = new HashMap<>();
		data.put("login_name", sysUser.getLogin_name());
		data.put("username", sysUser.getUsername());
		data.put("phone", sysUser.getPhone());
		data.put("email", sysUser.getEmail());
		data.put("user_kind", sysUser.getUser_kind());////用户类别：1市县用户、2乡镇用户、3园区用户、4农资用户、5质量检测、6企业用户
		rsmap.put("data", data);
		
		return rsmap;
	}
	
	
	/**
	 * 修改密码
	 * @param ssid
	 * @param oldpwd
	 * @param newpwd
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/updatePwd.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object updatePwd(String ssid, String oldpwd, String newpwd, HttpServletRequest request){
		Map<String, Object> rsmap = new HashMap<>();
		if(StringUtils.isEmpty(ssid) || StringUtils.isEmpty(oldpwd) || StringUtils.isEmpty(newpwd)){
			rsmap.put("code", 1);
			rsmap.put("message", "缺少参数！");
			return rsmap;
		}
		
		SysError info = sysUserService.updatePassword(ssid, oldpwd, newpwd, newpwd, request);
		if(info != null){
			rsmap.put("code", 1);
			rsmap.put("message", info.getMessage());
			return rsmap;
		}
		rsmap.put("code", 200);
		return rsmap;
	}
	
	
	/**
	 * 修改用户信息
	 * @param ssid
	 * @param username
	 * @param phone
	 * @param email
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/updateUserinfo.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object updateUserinfo(String ssid, String username, String phone, String email, HttpServletRequest request){
		Map<String, Object> rsmap = new HashMap<>();
		
		if(StringUtils.isEmpty(ssid)){
			rsmap.put("code", 1);
			rsmap.put("message", "缺少参数！");
			return rsmap;
		}
		
		SysUser sysUser = new SysUser();
		sysUser.setUserid(ssid);
		sysUser.setUsername(username);
		sysUser.setPhone(phone);
		sysUser.setEmail(email);
		
		SysError info = sysUserService.updateSelf(sysUser, request);
		if(info != null){
			rsmap.put("code", 1);
			rsmap.put("message", info.getMessage());
			return rsmap;
		}
		rsmap.put("code", 200);
		return rsmap;
	}
	
	
	/**
	 * 企业列表页
	 * @param model
	 * @param ssid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/companyList.do", method=RequestMethod.GET)
	public String companyList(ModelMap model, String ssid, HttpServletRequest request){
		model.put("ssid", ssid);
		return "app/companyList";
	}
	
	
	/**
	 * 企业列表数据
	 * @param ssid
	 * @param search
	 * @param page
	 * @param pageNum
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getCompanyList.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getCompanyList(String ssid, String search, Integer page, Integer pageNum, HttpServletRequest request){
		List<Map<String, Object>> rslist = new ArrayList<>();
		LoginUser loginUser = sysUserService.getLoginUser(ssid);
		
		PageParam pageParam = new PageParam();
		pageParam.setPage(page);
		pageParam.setPagesize(pageNum);
		
		System.out.println("CompanyList-search"+search);
		
		List<Company> list = companyService.listForApp(search, pageParam, loginUser);
		if(list != null){
			for(Company company : list){
				Map<String, Object> map = new HashMap<>();
				
				map.put("id", company.getCompanyid());
				map.put("name", company.getCompanyName());
				map.put("contactPerson", company.getContactPerson());
				map.put("phone", company.getPhone());
				
				rslist.add(map);
			}
		}
		
		return rslist;
	}
	
	
	/**
	 * 企业详情
	 * @param model
	 * @param ssid
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/companyDetail.do", method={RequestMethod.POST,RequestMethod.GET})
	public String companyDetail(ModelMap model, String ssid, String id, HttpServletRequest request){
		TraceCompany company = traceQueryService.getTraceCompany(id);
		model.put("company", company);
		return "app/companyDetail";
	}
	
	
	/**
	 * 园区列表页
	 * @param model
	 * @param ssid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/parkList.do", method=RequestMethod.GET)
	public String parkList(ModelMap model, String ssid, HttpServletRequest request){
		model.put("ssid", ssid);
		return "app/parkList";
	}
	
	
	/**
	 * 园区列表数据
	 * @param ssid
	 * @param search
	 * @param page
	 * @param pageNum
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getParkList.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getParkList(String ssid, String search, Integer page, Integer pageNum, HttpServletRequest request){
		List<Map<String, Object>> rslist = new ArrayList<>();
		LoginUser loginUser = sysUserService.getLoginUser(ssid);
		
		PageParam pageParam = new PageParam();
		pageParam.setPage(page);
		pageParam.setPagesize(pageNum);
		
		List<FarmPark> list = farmParkService.listForApp(search, pageParam, loginUser);
		if(list != null){
			for(FarmPark park : list){
				Map<String, Object> map = new HashMap<>();
				
				map.put("id", park.getParkid());
				map.put("name", park.getParkName());
				map.put("contactPerson", park.getContactPerson());
				map.put("phone", park.getPhone());
				
				rslist.add(map);
			}
		}
		
		return rslist;
	}
	
	
	/**
	 * 园区详情
	 * @param model
	 * @param ssid
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/parkDetail.do", method={RequestMethod.POST,RequestMethod.GET})
	public String parkDetail(ModelMap model, String ssid, String id, HttpServletRequest request){
		TracePark park = traceQueryService.getTracePark(id);

		model.put("park", park);
		return "app/parkDetail";
	}
	
	
	/**
	 * 农资厂商列表耶
	 * @param model
	 * @param ssid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/manufacturerList.do", method=RequestMethod.GET)
	public String manufacturerList(ModelMap model, String ssid, HttpServletRequest request){
		model.put("ssid", ssid);
		return "app/manufacturerList";
	}
	
	
	/**
	 * 农资厂商列表数据
	 * @param ssid
	 * @param search
	 * @param page
	 * @param pageNum
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getManufacturerList.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getManufacturerList(String ssid, String search, Integer page, Integer pageNum, HttpServletRequest request){
		List<Map<String, Object>> rslist = new ArrayList<>();
		LoginUser loginUser = sysUserService.getLoginUser(ssid);
		
		PageParam pageParam = new PageParam();
		pageParam.setPage(page);
		pageParam.setPagesize(pageNum);
		
		List<Manufacturer> list = manufacturerService.listForApp(search, pageParam, loginUser);
		if(list != null){
			for(Manufacturer manufacturer : list){
				Map<String, Object> map = new HashMap<>();
				
				map.put("id", manufacturer.getManufacturerid());
				map.put("name", manufacturer.getManufacturerName());
				map.put("contactPerson", manufacturer.getContactPerson());
				map.put("phone", manufacturer.getPhone());
				
				rslist.add(map);
			}
		}
		
		return rslist;
	}
	
	
	/**
	 * 农资厂商详情
	 * @param ssid
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/manufacturerDetail.do", method={RequestMethod.POST,RequestMethod.GET})
	public String manufacturerDetail(ModelMap model, String ssid, String id, HttpServletRequest request){
		Manufacturer manufacturer = manufacturerService.get(id);
		
		String area = "";
		String groupid = manufacturer.getGroupid();
		if(StringUtils.isNotEmpty(groupid) && groupid.length()>4){
			area = sysGroupService.getFullname(groupid.substring(0, groupid.length()-4), "");
		}
		String level = "";
		switch (manufacturer.getManufacturerLevel()) {
		case "0": level = "省级"; break;
		case "1": level = "市级"; break;
		case "2": level = "地级"; break;
		}
		String productivity = "";
		switch (manufacturer.getProductivity()) {
		case "0": productivity = "0-50"; break;
		case "1": productivity = "50-200"; break;
		case "2": productivity = "200-500"; break;
		case "3": productivity = "500以上"; break;
		}
		String ccc = "";
		switch (manufacturer.getCcc()) {
		case "0": ccc = "无公害农产品"; break;
		case "1": ccc = "绿色食品"; break;
		case "2": ccc = "有机食品"; break;
		}
		
		String imgurl = UploadFile.getFileUrl(UploadFile.DIR_MANUFACTURER_IMG, manufacturer.getImgurl());
		
		Map<String, Object> data = new HashMap<>();
		data.put("id", manufacturer.getManufacturerid());
		data.put("name", manufacturer.getManufacturerName());
		data.put("area", area);
		data.put("business", manufacturer.getBusinessNo());
		data.put("corporate", manufacturer.getCorporatePerson());
		data.put("contact", manufacturer.getContactPerson());
		data.put("phone", manufacturer.getPhone());
		data.put("address", manufacturer.getAddress());
		data.put("level", level);
		data.put("lng", manufacturer.getLng());
		data.put("lat", manufacturer.getLat());
		data.put("postcodes", manufacturer.getPostcodes());
		data.put("productivity", productivity);
		data.put("ccc", ccc);
		data.put("imgurl", imgurl);
		data.put("code", manufacturer.getManufacturerNo());
		data.put("remark", manufacturer.getRemark());
		
		model.put("manufacturer", data);
		
		return "app/manufacturerDetail";
	}
	
	
	/**
	 * 种植信息/养殖信息
	 * @param model
	 * @param ssid
	 * @param type 类型：1种植、2养殖、3加工
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/plantList.do", method=RequestMethod.GET)
	public String plantList(ModelMap model, String ssid, String type, HttpServletRequest request){
		model.put("ssid", ssid);
		model.put("type", type);
		return "app/plantList";
	}
	
	
	/**
	 * 获取种植信息
	 * @param ssid
	 * @param type 类型：1种植、2养殖、3加工
	 * @param parkid
	 * @param blockid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getPlantList.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getPlantList(String ssid, String type, String parkNameSearch, String blockNameSearch,
			Integer page, Integer pageNum, HttpServletRequest request){
		List<Map<String, Object>> rslist = new ArrayList<>();
		LoginUser loginUser = sysUserService.getLoginUser(ssid);
		
		PageParam pageParam = new PageParam();
		pageParam.setPage(page);
		pageParam.setPagesize(pageNum);
		List<ParkBlockPlantView> list = parkBlockPlantService.listForApp(parkNameSearch, blockNameSearch,type, pageParam, loginUser);
		if(list != null){
			for(ParkBlockPlantView plant : list){
				Map<String, Object> map = new HashMap<>();
				
				map.put("id", plant.getPlantid());
				map.put("cropName", plant.getCropName());
				map.put("parkName", plant.getParkName());
				map.put("blockName", plant.getBlockName());
				map.put("parkid",plant.getParkid());
				map.put("beginTime", DateUtil.fmtDate(plant.getBeginTime()));
				
				rslist.add(map);
			}
		}
		
		return rslist;
	}
	
	
	/**
	 * 种植信息详情
	 * @param model
	 * @param ssid
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/plantDetail.do", method={RequestMethod.POST,RequestMethod.GET})
	public String plantDetail(ModelMap model, String ssid, String id, HttpServletRequest request){
		ParkBlockPlantView plant = parkBlockPlantService.getPlantView(id);
		model.put("plant", plant);
		
		List<ProRecordView> proRecordList = proRecordService.listProRecord(id);
		model.put("proRecordList", proRecordList);
		return "app/plantDetail";
	}
	
	/**
	 * 种植环节详情
	 * @param model
	 * @param ssid
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/linkDetail.do", method={RequestMethod.POST,RequestMethod.GET})
	public String linkDetail(ModelMap model,String plantid, String ssid, String recordid, HttpServletRequest request){
		ParkBlockPlantView plant = parkBlockPlantService.getPlantView(plantid);
		model.put("plant", plant);
		ProRecord proRecord = proRecordService.getProRecord(recordid);
		model.put("proRecord", proRecord);
		return "app/linkDetail";
	}
	
	
	/**
	 * 园区数据
	 * @param ssid
	 * @param type 类型：1种植、2养殖、3加工
	 * @return
	 */
	@RequestMapping(value="/getParkData.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getParkData(String ssid, String type){
		List<Map<String, Object>> rslist = new ArrayList<>();
		LoginUser loginUser = sysUserService.getLoginUser(ssid);
		List<TreeNode> list = farmParkService.getUITreeDataType(loginUser, null, true);
		for(TreeNode treeNode : list){
			Map<String, Object> map = new HashMap<>();
			map.put("id", treeNode.getId());
			map.put("name", treeNode.getName());
			
			rslist.add(map);
		}
		return list;
	}
	
	/**
	 * 地块数据
	 * @param ssid
	 * @param type 类型：1种植、2养殖、3加工
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/getBlockData.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getBlockData(String ssid, String type, String parkid){
		List<Map<String, Object>> rslist = new ArrayList<>();
		LoginUser loginUser = sysUserService.getLoginUser(ssid);
		List<TreeNode> list = parkBlockService.getUITreeDataType(parkid, type, false, loginUser);
		for(TreeNode treeNode : list){
			Map<String, Object> map = new HashMap<>();
			map.put("id", treeNode.getId());
			map.put("name", treeNode.getName());
			
			rslist.add(map);
		}
		return list;
	}
	
	
	/**
	 * 作业类型
	 * @param ssid
	 * @param type
	 * @return
	 */
	@RequestMapping(value="/getLinkType.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getLinkType(String ssid, String type){
		List<Map<String, Object>> rslist = new ArrayList<>();
		if("1".equals(type)){
			String json = "[{\"id\":\"0\",\"name\":\"基本环节\"}," +
					"{\"id\":\"1\",\"name\":\"播种环节\"}," +
					"{\"id\":\"2\",\"name\":\"施肥环节\"}," +
					"{\"id\":\"3\",\"name\":\"植保环节\"}," +
					"{\"id\":\"4\",\"name\":\"收获环节\"}]";
			return JsonUtil.toList(json);
		}else if("2".equals(type)){
			String json = "[{\"id\":\"0\",\"name\":\"基本环节\"}," +
					"{\"id\":\"1\",\"name\":\"喂养环节\"}," +
					"{\"id\":\"2\",\"name\":\"防疫环节\"}," +
					"{\"id\":\"3\",\"name\":\"出栏环节\"}]";
			return JsonUtil.toList(json);
		}
		return rslist;
	}
	
	
	/**
	 * 获取作业环节
	 * @param ssid
	 * @param blockid
	 * @param linkType
	 * @return
	 */
	@RequestMapping(value="/getProLinkData.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getProLinkData(String ssid, String blockid, Integer linkType){
		List<Map<String, Object>> rslist = new ArrayList<>();
		ParkBlockPlantView plant = parkBlockPlantService.getCurrentPlantView(blockid);
		if(plant==null){
			return rslist;
		}
		
		if("1".equals(plant.getPlantType())){
			return productionLinkService.getProlinkData(plant.getCropid(), linkType);
		}else if("2".equals(plant.getPlantType())){
			return productionyzLinkService.getProlinkData(plant.getCropid(), linkType);
		}
		
		return rslist;
	}
	
	
	/**
	 * 获取物资信息
	 * @param ssid
	 * @param blockid
	 * @param linkType
	 * @return
	 */
	@RequestMapping(value="/getMaterialData.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getMaterialData(String ssid, String blockid, String linkType){
		List<Map<String, Object>> rslist = new ArrayList<>();
		ParkBlock block =  parkBlockService.get(blockid);
		if(block==null){
			return rslist;
		}
		List<Repertory> list = repertoryService.listRepertory(block.getFarmPark().getParkid(), block.getBlockType(), linkType);
		if(list != null){
			for(Repertory repertory : list){
				Map<String, Object> map = new HashMap<>();
				map.put("id", repertory.getRepertoryid());
				map.put("name", repertory.getMaterialName());
				
				rslist.add(map);
			}
		}
		return rslist;
	}
	
	
	
	
	/**
	 * 添加作业信息（无图）
	 * @param proRecord
	 * @param blockid
	 * @param ssid
	 * @param materialid
	 * @param materialAmount
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addProRecordNoimg.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object addProRecordNoimg(ProRecord proRecord, String blockid, String ssid,
			String materialid, Double materialAmount, HttpServletRequest request){
		return addProRecord(proRecord, blockid, ssid, materialid, materialAmount, null, null, null, request);
	}
	
	
	/**
	 * 添加作业信息
	 * @param proRecord
	 * @param blockid
	 * @param ssid
	 * @param materialid
	 * @param materialAmount
	 * @param imgfile1
	 * @param imgfile2
	 * @param imgfile3
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addProRecord.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object addProRecord(ProRecord proRecord, String blockid, String ssid,
			String materialid, Double materialAmount,
			MultipartFile imgfile1, MultipartFile imgfile2, MultipartFile imgfile3, HttpServletRequest request){
		Map<String, Object> rsmap = new HashMap<>();
		
		LoginUser loginUser = sysUserService.getLoginUser(ssid);
		
		ParkBlockPlantView plant = parkBlockPlantService.getCurrentPlantView(blockid);
		if(plant == null){
			rsmap.put("code", 1);
			rsmap.put("message", "添加失败，没有种植信息！");
			return rsmap;
		}
		
		if("1".equals(plant.getPlantType())){
			ProductionLink link = productionLinkService.get(proRecord.getLinkid());
			proRecord.setLinkName(link.getLinkName());
			proRecord.setLinkType(link.getLinkType());
		}else {
			ProductionyzLink link = productionyzLinkService.get(proRecord.getLinkid());
			proRecord.setLinkName(link.getLinkName());
			proRecord.setLinkType(link.getLinkType());
		}
		
		proRecord.setWorker(loginUser.getUserid());
		proRecord.setWorkerName(loginUser.getUsername());
		
		List<String> imgslist = new ArrayList<>();
		try {
			String img = saveUploadFile(imgfile1);
			if(StringUtils.isNotEmpty(img)){
				imgslist.add(img);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			String img = saveUploadFile(imgfile2);
			if(StringUtils.isNotEmpty(img)){
				imgslist.add(img);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			String img = saveUploadFile(imgfile3);
			if(StringUtils.isNotEmpty(img)){
				imgslist.add(img);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String[] imgs = imgslist.toArray(new String[imgslist.size()]);
		MyUploadFileParam uploadFileParam = new MyUploadFileParam(imgs, imgs, imgs);
		
		try {
			SysError info = proRecordService.add(proRecord, plant.getPlantid(), uploadFileParam, materialid, materialAmount, loginUser, request);
			if(info != null){
				rsmap.put("code", 1);
				rsmap.put("message", info.getMessage());
				return rsmap;
			}
		} catch (Exception e) {
			rsmap.put("code", 1);
			rsmap.put("message", e.getMessage());
			return rsmap;
		}
		
		rsmap.put("code", 200);
		return rsmap;
	}
	
	
	protected String saveUploadFile(MultipartFile file) throws IllegalStateException, IOException{
		if(file == null || file.getSize()==0){
			return null;
		}
		String saveDir = UploadFile.DIR_PRORECORD_IMG;
		
		String origName = file.getOriginalFilename();
		String ext = FilenameUtils.getExtension(origName).toLowerCase(Locale.ENGLISH);
		String fileSaveName = UploadUtil.generateDirFilename() +"." + ext;
		
		String dir = UploadFile.getSaveDirByKey(saveDir);
		String realPath = UploadFile.getFilerootRealpath(dir+"/"+fileSaveName);
		//目标文件对象
		File dest = new File(realPath);
		UploadUtil.checkDirAndCreate(dest.getParentFile());
		//保存文件
		file.transferTo(dest);
		return fileSaveName;
	}
	
	
}
